package com.ruoyi.project.kkad.goodsad.controller;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.kkad.goodsad.domain.GoodsAd;
import com.ruoyi.project.kkad.goodsad.service.IGoodsAdService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品广告Controller
 *
 * @author shixiangwen
 * @date 2021-08-06
 */
@Controller
@RequestMapping("/kkad/goodsad")
public class GoodsAdController extends BaseController {
    private String prefix = "kkad/goodsad";

    @Autowired
    private IGoodsAdService goodsAdService;

    @RequiresPermissions("kkad:goodsad:view")
    @GetMapping()
    public String goodsad() {
        return prefix + "/goodsad";
    }

    /**
     * 查询商品广告列表
     */
    @RequiresPermissions("kkad:goodsad:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(GoodsAd goodsAd) {
        startPage();
        List<GoodsAd> list = goodsAdService.selectGoodsAdList(goodsAd);
        return getDataTable(list);
    }

    /**
     * 导出商品广告列表
     */
    @RequiresPermissions("kkad:goodsad:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(GoodsAd goodsAd) {
        List<GoodsAd> list = goodsAdService.selectGoodsAdList(goodsAd);
        ExcelUtil<GoodsAd> util = new ExcelUtil<GoodsAd>(GoodsAd.class);
        return util.exportExcel(list, "goodsad");
    }

    /**
     * 新增商品广告
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存商品广告
     */
    @RequiresPermissions("kkad:goodsad:add")
    @Log(title = "商品广告", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(GoodsAd goodsAd) {
        return toAjax(goodsAdService.insertGoodsAd(goodsAd));
    }

    /**
     * 修改商品广告
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        GoodsAd goodsAd = goodsAdService.selectGoodsAdById(id);
        mmap.put("goodsAd", goodsAd);
        return prefix + "/edit";
    }

    /**
     * 修改保存商品广告
     */
    @RequiresPermissions("kkad:goodsad:edit")
    @Log(title = "商品广告", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(GoodsAd goodsAd) {
        return toAjax(goodsAdService.updateGoodsAd(goodsAd));
    }


    @GetMapping("/preview/{id}")
    public String preview(@PathVariable("id") Long id, ModelMap mmap) {
        GoodsAd goodsAd = goodsAdService.selectGoodsAdById(id);
        mmap.put("keyword", goodsAd.getTags());
        return prefix + "/preview";
    }

    /**
     * 删除商品广告
     */
    @RequiresPermissions("kkad:goodsad:remove")
    @Log(title = "商品广告", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(goodsAdService.deleteGoodsAdByIds(ids));
    }


    @RequiresPermissions("kkad:goodsad:edit")
    @Log(title = "商品广告", businessType = BusinessType.GRANT)
    @PostMapping("/updateStatus/{id}")
    @ResponseBody
    public AjaxResult updateStatus(@PathVariable("id") Long id, Integer status) {

        GoodsAd goodsAd = goodsAdService.selectGoodsAdById(id);
        goodsAd.setStatus(status);

        return toAjax(goodsAdService.updateGoodsAd(goodsAd));
    }
}
